<?php
/****************************************************
 *  �ļ�˵��: �����ķ�װ
 ***************************************************/
/**
 * ��ȡ���Ĳ����ַ�
 * ��ȡָ���ַ�ָ�����ȵĺ���,�ú�����Զ��ж���Ӣ��,�����������
 */
 function FSubstr($Str,$start,$strlen=10,$other=true)
 {
    global $s;
    $Length = $strlen;
    $i = 0;
    $l = 0;
    $ll = strlen($Str);
    $s = $Str;
    $f = true;
    while ($i <= $ll) {
        if (ord($Str{$i}) < 0x80) {
              $l++;
              $i++;
        }
        else if (ord($Str{$i}) < 0xe0) {
             $l++;
             $i += 2;
        }
        else if (ord($Str{$i}) < 0xf0) {
             $l += 2;
             $i += 3;
        }
        else if (ord($Str{$i}) < 0xf8) {
             $l += 1;
             $i += 4;
        }
        else if (ord($Str{$i}) < 0xfc) {
             $l += 1;
             $i += 5;
        }
        else if (ord($Str{$i}) < 0xfe) {
             $l += 1;
             $i += 6;
        }
        if (($l >= $Length - 1) && $f)  {
             $s = substr($Str, 0, $i);
             $f = false;
        }
        if (($l > $Length) && ($i < $ll)) {
             $s = $s . '...';
             break;
        }
    }
    return $s;
 }
  /**
  * ���ַ��е� ' \ תΪ��ݿ���֧�ֵ��ַ�
  * flag: 1 addslashes 2 stripslashes 3 strip_tags
  */
 function work_str($str, $flag=1)
 {
   //ħ�����
   if ($flag == 1)
   {
     if (get_magic_quotes_gpc())
	   return $str;
	 $value = addslashes($str);
   }
   else if ($flag == 2)
   {
	 $value = stripslashes($str);
   }
   else if ($flag == 3)
   {
     if (! get_magic_quotes_gpc())
	   $str = addslashes($str);
     $value = strip_tags($str);
   } //End if
   return $value;
 }

 /**
  * function:  ģ����֤
  * @param $Mod_Name  ģ�����
  */
  function Auth_Mod($Mod_Name)
  {
     global $conf, $con;
	 $USER_ID = Admin_User_ID();
	 if ($USER_ID == 2) return true;
     $query = "SELECT user_grant FROM " . $conf["tbl_prefix"] . "users WHERE user_id=$USER_ID";
	 $res = mysql_query($query);
	 if ($rows = mysql_fetch_array($res))
     {
	   $action_str = $rows["user_grant"];
	   $arr_action = explode(",", $action_str);
	   if (array_search($Mod_Name, $arr_action) === FALSE)
	   {
		  Alert("�ǳ���Ǹ, ��û��ʹ�ø�ģ���Ȩ��");
		  exit(0);
	   }
	   return true;
	 }
	 return false;
  }

   /**
   * ����: ����Ի���
   */
  function MyNotice($msg, $flag=1, $url="")
  {
    $script_str = "<script language='javascript'>";
	$script_str .= "alert('" . $msg . "');";
	//������һҳ
	if ($flag == 1)
    {
	  $script_str .= "window.history.back();";
	}
	else if ($flag == 2)
    {
	  $script_str .= "window.location.reload('$url');";
	}
	$script_str .= "</script>";
	echo $script_str;
  }
  /*
  **���ܣ������µĴ���
  */
  function create_new_window($url)
  {
		$script_str="<script language='javascript'>";
		$script_str .="window.open('".$url."','_blank')";
		$script_str .="</script>";
		echo $script_str;
  }
  function verdict_browser()
  {
	  $script_str="<script language='javascript'>";
	  $script_str .="return navigator.appVersion";
	  $script_str .="</script>";
  }
  /**
   * ��ȡ�÷��������е������ӷ���,���ID��
   */
  function get_News_Sub(&$muster, $fid)
  {
    global $con, $conf;
    $sql = "select cate_id from " . $conf["tbl_prefix"] . "news_cate where parent_id IN ( $fid) ORDER BY cate_order";
    $result1 = mysql_query($sql, $con) or die(mysql_error());
    if ($myrow1 = @mysql_fetch_array($result1))
    {
      do {
   	   $cate_id = $myrow1["cate_id"];
       $muster .= "," . $cate_id;
       get_News_Sub(&$muster, $myrow1["cate_id"]);
      } while ($myrow1=mysql_fetch_array($result1));
    }
    return $muster;
  }

    /**
   * ��ȡ�÷��������еĽ��ڷ���,���ID��
   */
  function get_Pros_Sub(&$muster, $fid)
  {
    global $con, $conf;
    $sql = "select cate_id from " . $conf["tbl_prefix"] . "hou_cate where parent_id IN ($fid) ORDER BY cate_order";
    $result1 = mysql_query($sql, $con) or die(mysql_error());
    if ($myrow1 = @mysql_fetch_array($result1))
    {
      do {
   	   $cate_id = $myrow1["cate_id"];
       $muster .= "," . $cate_id;
       get_News_Sub(&$muster, $myrow1["cate_id"]);
      } while ($myrow1=mysql_fetch_array($result1));
    }
    return $muster;
  }


  /**
   * function ��$fid����������
   *
   */
  function get_cate_sub( $fid)
  {
    global $con, $conf;
    $sql = "select cate_id from " . $conf["tbl_prefix"] . "news_cate where parent_id IN ( $fid) ORDER BY cate_order";
    $result = mysql_query($sql, $con) or die(mysql_error());
    while( $array = mysql_fetch_array( $result))
	{
		$muster .= $array['cate_id'] . "," . get_cate_sub( $array['cate_id']);
	}
    return $muster . $fid;
  }


  /**
   * ���ID��,��ȡ���������еĸ�����
   */
  function get_News_Parent(&$muster,$fid)
  {
    global $con, $conf;
    $sql = "select parent_id from " . $conf["tbl_prefix"] . "news_cate where cate_id = $fid order by parent_id desc";
    $result1 = mysql_query($sql, $con) or die(mysql_error());
    if ($myrow1 = @mysql_fetch_array($result1))
    {
      do {
   	   $parent = $myrow1["parent_id"];
	   if ( $parent != 0 )
         $muster .= "," . $parent;
       get_News_Parent($muster, $myrow1["parent_id"]);
      } while ($myrow1=mysql_fetch_array($result1));
    }
	return $muster;
  }



  /**
   * ���ID��,��ȡ��ũ�������еĸ�����
   */
  function get_Pro_Parent(&$muster,$fid)
  {
    global $con, $conf;
    $sql = "select parent_id from " . $conf["tbl_prefix"] . "hou_cate where cate_id = $fid order by parent_id desc";
    $result1 = mysql_query($sql, $con) or die(mysql_error());
    if ($myrow1 = @mysql_fetch_array($result1))
    {
      do {
   	   $parent = $myrow1["parent_id"];
	   if ( $parent != 0 )
         $muster .= "," . $parent;
       get_Pro_Parent($muster, $myrow1["parent_id"]);
      } while ($myrow1=mysql_fetch_array($result1));
    }
	return $muster;
  }




 /**
  * function: ִ��mysql��ݿ��ѯ
  * @param $query ����Ĳ�ѯ���
  * @param $flag ���flag������Ӧ���: 0,���ؽ����Դ 1,���ؽ����Ŀ 2,���ؽ������ 3,����Ψһ���
  *
  */
  function do_query($query,$flag)
  {
	  $result = mysql_query($query) or die(mysql_error());

	  switch($flag)
	  {
		  case 0 :
			      $return = $result;
		          break;
		  case 1 :
			      $return = mysql_num_rows($result);
		          mysql_free_result($result);
		          break;
          case 2 :
			      $return = mysql_fetch_array($result);
		          mysql_free_result($result);
		          break;
		  case 3 :
			     $array = mysql_fetch_array($result);
		         mysql_free_result($result);
				 $return = $array[0];
				 break;
		  default :
			     $return = $query;
	  }
	  return $return;
  }


 /**
 *  function ����: ���Ҫ��ѯ���ֶ�,����ֶ�,���ֵ,��,�õ�ֵ
 *  ��:  ��$tab���в�ѯ$field�ֶε�ֵ,Ҫ��$s_val�ֶε�ֵ����$s_val,����$sql_linkΪ��MYSQL��l��
 *         $flag:��ʶ  0:����ÿһ��Ԫ�� 1:���ؼ�¼�� 2:��������Ԫ������,һ���ά�� 3 ���ؼ�¼�� 4 ���ز�ѯ��� 5 ���ؽ����Դ
 **/
function get_field_val($field,$s_field,$s_val,$tab,$flag=0)
{
   $query="select $field from $tab where $s_field='$s_val'";

   if ($flag == 4) return $query;

   $res = mysql_query($query) or die();
   $rnum = mysql_num_rows($res); //��¼��

   if(1 == $flag) return $rnum;

   if( $rnum > 0) //�м�¼
   {
	  $num=0;
	  if( $flag == 5)
	  {
		  return $res;
	  }
	  while(($row = mysql_fetch_array($res)))
      {
	     if($flag == 0)
  			return $row[0];
		 if ($flag == 3)
            return $row;
	     $fielc_num = mysql_num_fields($res); //�ֶ���
	     for($i=0; $i<$fielc_num; $i++)
		 {
		    $ay[$num][$i]=$row[$i];
		 }
    	 $num++;
	  }//end while
	 return $ay;
   }
   else
   {
     return FALSE; //û�м�¼
   }//end if
 }



  /**
   * ��ȡ����t����DƬt��
   *  flag :     1 ����t�� 2 �DƬt�� 3 ȫ����ʾ
   *  cols_num:  ÿһ����ʾ�ĸ���
   *  total:     ��ʾ����t�ӵĸ���
   */
  function get_Links($flag = 2, $cols_num=6, $Total_num="")
  {
    global $conf;
    $TBL_LINKS = $conf["tbl_prefix"] . "links";                //����t�ӱ�
	$Total_num = empty($Total_num) ? "" : "LIMIT $Total_num";  //��ʾ����t�ӵĸ���
	$link_str = "<table cellSpacing=1 cellPadding=3 width=\"100%\" border='0' class='tablenone'>";  //���ʼ
  /* �DƬt�� */
	if (($flag == 2) || ($flag == 3))
	{
      $query = "SELECT * FROM $TBL_LINKS WHERE link_type=2 ORDER BY link_top $Total_num";
      $res = mysql_query($query);
      $Q_num = mysql_num_rows($res);           //�DƬt�ӵ��ܸ���
      $rows_num = ceil($Q_num / $cols_num);    //��ʾ�е���Ŀ
      $image_link = "";
      for ($i=1; $i<=$rows_num; $i++)
      {
        $image_link .= "<tr>";
		$val_ext = ($i - 1) * $cols_num;
        for ($j = 1; $j <= $cols_num; $j++)
	    {
		  $val = $val_ext + $j;    //��*��
		  if ($val > $Q_num)
		  {
		    $image_link .= "<td align='left' bgColor='#FFFFFF'></td>";
			continue;
		  }
	      $row = mysql_fetch_array($res);
	      $image_link .= "<td align=left bgColor=#FFFFFF>";
  	      $link_title = $row["link_title"];  //���
          $link_url = $row["link_url"];  //t�ӵ�ַ
		  $link_logo_src = $row["link_logo_src"];  //t�ӈDƬ��ַ
	      $ext = strrchr($link_logo_src, ".");      //��ȡ)չ��
 	      $swf = false;
	      if ($ext == ".swf")        //flash �ļ�
		  {
		     $swf = true;
		  }
 		  if ($swf == true)  //��ʾflasht��
		  {
	        $image_link .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="88" height="31">
                                  <param name="movie" value="' . $link_logo_src . '">
                                  <param name="quality" value="high">
                                  <param name="menu" value="false">
                                  <embed src="http://www.hqbonline.com/images/logo.swf" width="88" height="31" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" menu="false">
                                  </embed></object>';
	      } else {                 //��ʾ�DƬt��
		    $image_link .= '<a href="' . $link_url . '" target="_blank" title="' . $link_title . '"><img border="0" src="'.$link_logo_src.'" width="88" height="31"></a>';
	      }
          $image_link .= "</td>";
       }
	   $image_link .= "</tr>";
    }
  }
 /* ����t�� */
  if (($flag == 1)  || ($flag == 3))
  {
     $query = "select * from $TBL_LINKS where link_type=1 order by link_top $Total_num";
     $res = mysql_query($query);
     $Q_num = mysql_num_rows($res);  //����t����
     $rows_num = ceil($Q_num / $cols_num);   //��ʾ�е���Ŀ
     for ($i = 1;$i <= $rows_num;$i++)
     {
       $text_link .= "<tr>";
   	   $val_ext = ($i - 1) * $cols_num;
       for ($j = 1;$j <= $cols_num;$j++)
	   {
		 $val = $val_ext + $j;    //��*��
		 if ($val > $Q_num)
		 {
		   $image_link .= "<td align='center'></td>";
		   continue;
		 }
	     $row = mysql_fetch_array($res);
	     $text_link .= "<td align=center>";
 	     $full_link_title = $row["link_title"];  //���
 	     $link_title = $row["link_title"];  //���
         $link_url = $row["link_url"];  //t�ӵ�ַ
   	     $text_link .= "<a href=\"" . $link_url . "\" target=\"_blank\" title='" . $full_link_title . "'>" . $link_title . "</a><br>";
		 $text_link .= "</td>";
	   }
	   $text_link .= "</tr>";
     }
  }
  if ($flag == 1)
    $image_link = "";
  if ($flag == 2)
    $text_link = "";
  $link_str .= $image_link . $text_link . "</table>";
  return $link_str;
 }


  /**
   * function: ���ID��,��ȡ���еĸ�����,��ɵ���
   * @param $fid ����ID
   * @param $url ������Ҫ�����t�ӵ�ַ,��(��׺
   *
   */
  function get_parent_title( $fid, $url)
  {
    global $con, $conf;

    $query = "SELECT cate_id,parent_id,zh_cate_title FROM " . $conf["tbl_prefix"] . "news_cate WHERE cate_id='$fid' ORDER BY parent_id DESC";

    $result = mysql_query( $query) or die(mysql_error());
	while( $array = mysql_fetch_array( $result))
	{
	   $this_url = sprintf( $url, $array['cate_id'], $array['parent_id']);
	   $cate_title = "<a href='$this_url' style='color:red;font-weight:bold;font-size:13px'>" . $array['zh_cate_title'] . "</a>";
	   return get_parent_title( $array['parent_id'], $url) . "&nbsp;" . ">>" . "&nbsp;" . $cate_title;
	}
  }


  /**
 *  ����: ���Ҫ��ѯ���ֶ�,����ֶ�,���ֵ,��,�õ�ֵ
 *  ��:  ��$tab���в�ѯ$field�ֶε�ֵ,Ҫ��$s_val�ֶε�ֵ����$s_val,����$sql_linkΪ��MYSQL��l��
 *         $falg:��ʶ  0:����ÿһ��Ԫ�� 1:���ؼ�¼�� 2:��������Ԫ������,һ���ά�� 3 ���ؼ�¼�� 4 ���ز�ѯ���
 **/
function get_owner_val($field,$s_field,$s_val,$tab,$falg=0)
{
   global $conf;
   if(is_numeric($s_val) == TRUE) //���������
      $query="select $field from $tab where $s_field=$s_val";
   else $query="select $field from $tab where $s_field='$s_val'";
   if ($falg == 4) return $query;
   $res = mysql_query($query);
   $rnum = mysql_num_rows($res); //��¼��
   if($falg == 1) return $rnum;
   if( $rnum > 0) //�м�¼
   {
	  $num=0;
	  while(($row = mysql_fetch_array($res)))
      {
	     if($falg == 0)
  			return $row[0];
		 if ($falg == 3)
            return $row;
	     $fielc_num = mysql_num_fields($res); //�ֶ���
	     for($i=0; $i<$fielc_num; $i++)
		 {
		    $ay[$num][$i]=$row[$i];
		 }
    	 $num++;
	  }//end while
	 return $ay;
   } else {
     return FALSE; //û�м�¼
   }//end if
 }

/**
 * ��������, �����ά����
 *
 * @param  string $muster HTML����
 * @param  int    $fid    ����ID
 * @return string ������-ʽ�˵�
 */
 function genCateArray($muster, $fid, $level=1)
 {
    global $conf;
    $sql = "SELECT * FROM " . $conf["tbl_prefix"] . "news_cate WHERE parent_id = $fid ORDER BY cate_order DESC";
    $result1 = mysql_query($sql) or die(mysql_error());
    while ($myrow1 = mysql_fetch_array($result1))
    {
	  $cate_id = $myrow1['cate_id'];
      $muster[$fid][$cate_id]['cate_id'] = $cate_id;
      $muster[$fid][$cate_id]['c_name'] = $myrow1['zh_cate_title'];   //�������
	  $muster[$fid][$cate_id]['sort_id'] = $myrow1['cate_order']; //˳��
	  $muster[$fid][$cate_id]['level'] = $level; //�ڼ�������
      genCateArray(&$muster, $cate_id, $level+1);
    }
    return $muster;
 }


/**
 * ��ɷ�����-�˵�
 *
 * @param  array $Data
 * @return string  HTML����
 */
 function genCateTree($Data, $Level)
 {
    global $gOptions;
	foreach($Data[$Level] as $key => $value)
	{
	  $space = "|" . str_repeat('-', $value['level']);
	  if (is_array($Data[$key]))
	  {
	    $title = $space . $value['c_name'];
        $sub = $value['cate_id'];
		$gOptions .= "<option value='$sub'>" . $title . "</option>\n";
	    genCateTree($Data, $key);
	  }
	  else
	  {
	    $title = $space . $value['c_name'];
		$sub = $value['cate_id'];
		$gOptions .= "<option value='$sub'>" . $title . "</option>\n";
	  }
	}
 }


 /**
  * function ����������option
  * param $array ����
  * param $value ѡ��ֵ
  */
  function mkoptions( $array,$value)
  {
	  foreach( $array as $key => $name)
	  {
		  $select = ( $key == $value) ? "selected=\"selected\"" : "";
		  $option .= "<option value=$key $select>$name</option>\n";
	  }
	  return $option;
  }

/**
 * ��ȡ��ǰ����
 */
 function get_Date()
 {
   return date("Y-m-d", time());
 }
 /**
  * ��ȡ��ǰʱ������
  */
 function get_Now()
 {
   return date("Y-m-d H:i:s", time());
 }
 /**
  * ���������ĸ�����ֵ����
  *
  * @param  int $length ȡ��λ��
  * @return int
  */
  function genRand($length=5)
  {
    $chars = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
    for($i=0; $i<strlen($chars); $i++)
    {
       $arr[$i] = $chars[$i];
    }
    srand((float)microtime()*1000000);
    shuffle($arr);
    $reval = substr(implode("", $arr), 5, $length);
	return $reval;
  }
  /**
  * �������õ�ֵ
  *
  * @param $Config_name �������
  */
  function get_Config_Value($Config_name)
  {
     global $conf;
	 $query = "SELECT * FROM ".$conf["tbl_prefix"]."config WHERE config_name='$Config_name'";
	 $res = mysql_query($query);
	 if ($rows = mysql_fetch_array($res))
     {
	   return $rows["config_value"];
	 }
	 return false;
  }
 /**
  * function: ���ػ�ԱID
  */
  function admin_user_id()
  {
    global $userdata;
	$user_id = $userdata["user_id"];
	return $user_id < 1 ? 0 : $user_id;
  }

 /**
  * function: ���ػ�Ա��
  */
 function admin_UserName()
 {
   global $userdata;
   $username = $userdata["username"];
   return $username;
 }

 /**
  * ��������������Ϣ
  * @return array
  */
  function get_sysConfig()
  {
    global $conf;
    $TBL_CONFIG = $conf["tbl_prefix"] . "config";   //�����ļ�
	$query = "SELECT * FROM $TBL_CONFIG";
	$res = mysql_query($query);
	while ($rows = mysql_fetch_array($res))
	{
	  $config_name  = $rows["config_name"];
	  $config_value = $rows["config_value"];
	  $conf_data[$config_name] = $config_value;
	}
	return $conf_data;
  }
    /**
	* function: �������е�Ʒ��
	*/
   function get_Brands()
   {
	  global $conf, $con;
	  $query = "SELECT * FROM " . $conf["tbl_prefix"] . "brands ORDER BY brand_order";
	  $res = mysql_query($query);
	  return $res;
   }

 /************************************************************************
  * ��ɵ�ѡ��ť��,$array����ʾ���,$name�ǻ�ȡ��������,$value�ǳ�ʼֵ
  *
  ************************************************************************/
 function mkradio($array,$name,$value,$begin=0,$headstr,$footstr)
  {
	  foreach($array as $radio)
	  {
		  if($begin == $value)
		  {
			  $str .= $headstr."<input type=\"radio\" name=\"$name\" value=\"$begin\" checked=\"checked\">$radio".$footstr;
		  }
		  else
		  {
			  $str .= $headstr."<input type=\"radio\" name=\"$name\" value=\"$begin\">$radio".$footstr;
		  }
		  $begin++;
	  }
	  return $str;
 }
 /**
  * ��ȡ����IDֵ
  */
 function get_max_id($field_name,$table_name,$sql_link) //���������ֶ�ID��
 {
       $query="select max($field_name) from $table_name";
	   $res = mysql_query($query,$sql_link);
	   $row = mysql_fetch_array($res);
	   return $row[0];
 }
  /**
   * ��ȡ��Ƭ������������е�Ƭ���ӷ���,���ID��
   */
  function get_Teams_Sub(&$muster, $fid)
  {
    global $con, $conf;
    $sql = "select team_id from " . $conf["tbl_prefix"] . "teams where supr_id = $fid order by team_id desc";
    $result1 = mysql_query($sql, $con) or die(mysql_error());
    if ($myrow1 = @mysql_fetch_array($result1))
    {
      do {
   	   $team_id = $myrow1["team_id"];
       $muster .= "," . $team_id;
       get_Teams_Sub(&$muster, $myrow1["team_id"]);
      } while ($myrow1=mysql_fetch_array($result1));
    }
    return $muster;
  }
 /**
  * �O��ҳ�滺��
  */
 function set_cache()
 {
   session_cache_limiter('private,must-revalide');
 }
	/**
	 * function: ��ɲ�ѯ�ͻ���ϸ���ϵ����
	 * @param string $which ��Ҫ�Ŀͻ�
	 *
	 *
	 */
	 function query_customer( $which="user_rate!=10")
	 {
		 global $conf;

		 $TBL_USERS = $conf['tbl_prefix'] . "users";
		 $TBL_CUSTOMER = $conf['tbl_prefix'] . "customer";

		 $query = "SELECT users.user_id,username,user_realname,user_regdate,cu.* FROM $TBL_USERS users,$TBL_CUSTOMER cu WHERE $which AND users.user_rank=5 AND users.post_status=1 AND users.user_id=cu.user_id ORDER BY users.user_id DESC";
		 return $query;
	 }
 /**
  * ͨ��ű��ڿ���е������´�ҳ��,
  */
  function frame_open($url)
  {
    echo "<SCRIPT LANGUAGE='javascript'>";
    echo "<!-- \n";
    echo "top.location.href='$url'";
    echo "\n-->";
    echo "</SCRIPT>";
  }
?>